home *** CD-ROM | disk | FTP | other *** search
-
- /*
-
- xmodem2.c
-
-
- 8/1/85
-
-
- A version of Ward Christensen's MODEM program modified
- by Larry Jordan Associates to include XMODEM features
- and to provide compatibility with TCOMM.
-
- This file contains the Xmodem support functions. Functions
- that are part of the The Greenleaf Functions commercial
- libraries are marked in the FUNCTION.H file. You must
- buy the Greenleaf Comm Library and General Library to
- get the source and object code for these functions.
-
- Copyright (c) 1984, 1985 Larry Jordan Associates
- */
-
- #include <stdio.h> /* Lattice header */
- #include <gleafs.h> /* Greenleaf header */
- #include <timedate.h> /* Greenleaf header */
- #include <asiports.h> /* Greenleaf header */
- #include <function.h> /* Xmodem header provided on disk */
- #include <ibmpcio.h> /* Xmodem header provided on disk */
- #include <protocol.h> /* Xmodem header provided on disk */
-
- /* makebuff
- *
- * communications buffer initialization
- * Initialize comm port
- *
- *
- * Returns: None
- *
- */
-
- int makebuff()
- {
- extern int port,bps,parity,stopbits,wordlen;
- extern char recvbuff[],txbuff[];
-
- int status,stat,n;
-
- stat = asisetup(port, ASINOUT, BINARYNONE, recvbuff, 500, txbuff, 500 ); /* set up interrupt buffers */
- if ( stat == -1 ) return(ERROR);
- asdtr(port,ON); /* be sure DTR is on */
- asrts(port,ON); /* be sure RTS is on */
-
- stat = asiinit( port, bps, parity, stopbits, wordlen ); /* initialize port */
- if ( stat == -1 ) return(ERROR);
- stat = asistart( port, ASINOUT ); /* start IN & OUT buffers */
- if ( stat == -1 ) return(ERROR);
-
- asixrst(port); /* read and reset .xmcount receiver char count. */
-
- stat = asiclear(port, ASINOUT); /* clear buffers to start */
- if(stat != -1) return(TRUE);
- asiquit(port); /* could not start clear buffers after start, so stop them and go home */
- return(ERROR);
-
- }
-
-
-
-
- /* miready
- *
- * Routine to return TRUE if modem is ready to input
- * a byte
- *
- * Returns: Serial port receive character register status
- */
-
- int miready()
- {
- extern int port;
-
- if ((asi_parms[port].flags & 2)==0) return(TRUE); /* if receiver has a character */
- return(FALSE);
-
- }
-
-
- /*
- * global exit that turns off interupts first.
- *
- *
- */
-
- int endxm()
- {
- extern int port,actpage,monitor;
-
- asiquit(port); /* turn interrupts off */
- delay(10);
- curtype(BLANK,6,7); /* turn cursor off for now */
- exit(0); /* quit normal */
- }
-
-
-
- /*
- * T I C K S
- *
- * Returns total number of clock ticks since midnight
- */
- long int ticks()
- {
-
- struct REGS regs; /* we need a place to put results */
-
- union { int s[2]; long ll;} secs ;
-
- regs.ax = 0;
- secs.ll = 0L;
- int86(0x1A,®s,®s); /* use Lattice interrupt function */
- secs.s[0] = regs.dx; /* High count */
- secs.s[1] = regs.cx; /* Low count */
- return(secs.ll);
- }
-
- /*
- * T O T S E C
- *
- * Time since midnight in seconds
- */
- long int totsec()
- {
-
- long int ticks(), secnds;
-
- secnds = (10l * ticks() ) / 182;
- return(secnds);
- }
-
-
- /*
- *
- * time hundreths of seconds
- *
- *
- *
- *
- */
-
- long int tothsec()
-
- {
-
- struct TIMEDATE *ptd;
-
- long int timehsec;
- int tsec,thsec;
-
-
- ptd = sgettime(7); /* get the time from system */
-
- tsec = ptd->seconds;
- thsec = ptd->hsecs;
-
- timehsec=tsec*100+thsec;
-
- return(timehsec);
-
- }
-
- /* Copyright (c) 1984, Larry Jordan Associates */
-
- /*
- *
- *
- * delay for n hundreths of seconds
- *
- *
- *
- *
- */
-
- delay(nhsec)
-
- int nhsec;
- {
-
- long int strtime;
- long int endtime;
- long int nowtime;
-
- strtime = tothsec();
- endtime = strtime + nhsec;
-
- while (TRUE)
- {
- nowtime = tothsec();
- if(nowtime < strtime)
- nowtime += 6000;
-
- if (nowtime > endtime)
- break;
- }
-
- }
-
-
- /*
- * toolong
- *
- * Timeout for user input with tout in seconds
- *
- * Return: FALSE = time not up
- * TRUE = time is up
- */
-
- int toolong(tout)
- int tout;
- {
-
- extern long int strtout;
- long int nowtime;
- long int elaptime;
-
- long int totsec();
-
- nowtime = totsec();
- if(nowtime < strtout) /* how does present time compare to start time mark */
- nowtime += 86400; /* past midnight, so add 24 hr of secs */
- elaptime = nowtime - strtout;
- if(elaptime < tout) /* is elapsed time greater than allowed time? */
- {
- return(FALSE);
- }
- else
- {
- return(TRUE);
- }
-
- }
-
-
-
- /*
- * readbyte
- *
- * read byte from port with timeout if nothing comes in
- *
- */
- int readbyte(timeout)
- int timeout;
- {
- extern long int strtout;
- extern char retstr[80];
- extern int port;
-
- int rtrn;
-
- if (miready()) /* is a char in buffer? */
- {
- rtrn = asigetc( port ); /* get it from buffer */
- rtrn &= 0x00ff;
- retstr[0] = rtrn; /* put char in a string buffer */
- retstr[1] = '\0';
- return(TRUE);
- }
-
- strtout = totsec(); /* reset start time mark */
-
- while(TRUE) /* while there is something there to get, get it */
- {
- if (miready()) /* if there is a char in buffer */
- {
- rtrn = asigetc( port ); /* get it into rtrn */
- break; /* and get out of loop */
- }
- if(toolong(timeout)) /* have we waited longer than we wanted to? */
- {
- prosay(19,45," - timeout");
- return(TIMEOUT);
- }
- if (asikbhit()) /* if SYSOP signalled, send his signal back */
- {
- if ( getch() == ESC) return(ESC);
- }
- if (!carrier()) return(CDLOST); /* if carrier was lost, tell calling function */
- }
- rtrn &= 0x00ff;
- retstr[0] = rtrn; /* put char into string buffer */
- retstr[1] = '\0'; /* be sure to terminate all strings */
- return(TRUE);
- }
-
-
-
- /* chrput
- *
- * If not in local mode and if carrier is present,
- * output a byte through serial port
- *
- * chrput()
- *
- * Returns: FALSE if carrier is not present
- */
-
- int chrput(c)
- char c;
- {
-
- extern int port;
-
- long int i;
-
- if(!carrier()) return(CDLOST);
- while( (asiputc(port,c) == -1) )
- {
- if(i++ > 100000L) return(CDLOST); /* Try it a few more times */
- }
-
- }
-
-
- /* strput
- *
- * If not in local mode and if carrier is present,
- * output a string through serial port
- *
- * Returns: FALSE if carrier is not present
- */
-
- int strput(string)
- char *string;
- {
- int i;
- int rtrn;
- char *sptr;
-
- for(sptr=string, i=0 ; i < 132 ;sptr++, i++)
- {
- if (chrput(*sptr) == CDLOST) return(FALSE);
- if (miready()) /* is a char in buffer? */
- {
- rtrn = asigetc( port ); /* get it from buffer */
- rtrn &= 0x00ff;
- if (rtrn == CTRLX || rtrn == NAK)
- {
- retstr[0] = rtrn; /* put char in a string buffer */
- retstr[1] = '\0';
- return(FALSE);
- }
- }
- }
- return(TRUE);
-
- /*........................................................
- int i;
-
- for (string,i=0 ; i < 132; i++, string++)
- {
- if(chrput(*string) == CDLOST)
- return(FALSE);
- }
- .............................................................*/
-
- }
-
-
-
- /* eatbuf
- *
- * Flush communications input buffer while carrier and receive buffer set
- *
- * Returns: None
- */
-
- int eatbuf()
- {
-
- extern int port;
-
- while(miready()) asigetc(port);
- }
-
-
- /* carrier
- *
- * check for existance of modem carrier signal
- *
- * carrier()
- *
- * Returns: TRUE if carrier present; else returns FALSE
- */
-
- int carrier()
- {
- int status;
-
- status = asistat(port); /* what is modem status? */
- if (status & 0x0080) return(TRUE); /* check rec line sig detect */
- return(FALSE); /* return FALSE if no carrier */
-
- }
-
- /*
- * prosay
- *
- * full screen color display for protocol info
- *
- */
- int prosay(irow,icol,str)
- int irow,icol;
- char *str;
- {
- extern int display;
- extern int cfgcolor;
-
- int row,col;
- char c;
- unsigned uret;
- char *p;
-
- if ( !display) return; /* if display is off from TCOMM, don't print char */
- row = irow;
- col = icol;
-
- p=str;
- while (*p)
- {
- curset(row,col,0); /* first, position cursor for output */
- c=*p++;
- cpch(c,cfgcolor,0x00,0); /* Green leaf fast display with color */
-
- uret = getcur(0); /* where are we on local screen */
- row = (uret & 0xFF00) >> 8;
- col = (uret & 0xFF) + 1;
- }
-
- }